package com.ruoyi.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;


import com.ruoyi.product.domain.WoSub;
import com.ruoyi.product.domain.WoSubFault;
import com.ruoyi.product.domain.WorkOrder;
import com.ruoyi.product.domain.dto.WoSubDto;
import com.ruoyi.product.domain.vo.WoSubVo;
import com.ruoyi.product.mapper.WoSubMapper;
import com.ruoyi.product.mapper.WorkOrderMapper;
import com.ruoyi.product.service.WoRawSubService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 *
 */
@Service
public class WoRawSubServiceImpl implements WoRawSubService {

    @Resource
    WoSubMapper woRawSubMapper;

    @Resource
    WorkOrderMapper workOrderMapper;


    @Override
    public Page<WoSubVo> getWoRawSubByPage(WoSubDto pageEntity) {
        MPJLambdaWrapper<WoSub> wrapper = new MPJLambdaWrapper<WoSub>()
                .selectAll(WoSub.class)//查询user表全部字段
                //记的测试阶段
                .eq(WoSub::getDelFlag, 0)
                .eq(Objects.nonNull(pageEntity.getWo()), WoSub::getWo, pageEntity.getWo())
                .orderByAsc(WoSub::getSequence, WoSub::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, WoSub.class, pageEntity);
        Page<WoSubVo> userList = woRawSubMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   WoSubVo.class, wrapper);
        return userList;
    }

    @Override
    public void addWoRawSub(WoSubDto testDto) {

        /**
         * 先查询工单
         */
        MPJLambdaWrapper<WorkOrder> wrapper = new MPJLambdaWrapper<WorkOrder>()
                .eq(WorkOrder::getWo, testDto.getWo())
                .ne(WoSub::getDelFlag, 1);


        WorkOrder workOrder = workOrderMapper.selectOne(wrapper);

        if(null != workOrder) {
            throw new GlobalException("没有该工单请重新查询");
        }

        Integer maxCount = workOrder.getMaxCount() + 1;
        StringBuilder stringBuilder = new StringBuilder();
        //2位数
        stringBuilder.append(String.format("%02d",Integer.valueOf(maxCount)));

        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<WoSub> wrapper2 = new MPJLambdaWrapper<WoSub>()
                .eq(WoSub::getCode, testDto.getCode())
                .ne(WoSub::getDelFlag, 1);
        WoSub material = woRawSubMapper.selectOne(wrapper2);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        WoSub test2 = new WoSub();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setDelFlag(false);
        test2.setSerialNo(workOrder.getBatchCode() + stringBuilder.toString());
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        woRawSubMapper.insert(test2);
        workOrder.setMaxCount(maxCount);
        workOrderMapper.insert(workOrder);
    }

    @Override
    public void updateWoRawSub(WoSubDto testDto) {
        MPJLambdaWrapper<WoSub> wrapper = new MPJLambdaWrapper<WoSub>()
                .eq(WoSub::getCode, testDto.getCode());
        WoSub material = woRawSubMapper.selectOne(wrapper);
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, material);
        material.setCreateBy("admin");
        material.setCreateTime(new Date());
        woRawSubMapper.updateById(material);
    }

    @Override
    public void delWoRawSub(WoSubDto testDto) {
        WoSub material = woRawSubMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        woRawSubMapper.updateById(material);
    }
}




